package cdworks.lifecounter;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class ActivityCount extends Activity {
	private String type;
	private LCDbManager manager;
	private String sign;
	private EditText et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_count);
		Intent intent = getIntent();
		
		if (intent.hasExtra(ActivityIn.AMOUNT_TYPE_IN)){
			type = intent.getStringExtra(ActivityIn.AMOUNT_TYPE_IN);
			sign = "+";
		} else {
			type = intent.getStringExtra(ActivityOut.AMOUNT_TYPE_OUT);
			sign = "-";
		}
		
		manager = new LCDbManager(this);
		et = (EditText)findViewById(R.id.count_numbers);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_count, menu);
		return true;
	}
	
	public void enter(View view) {
		String text = et.getText().toString();
		double amount;
		if (text.equals(""))
			return;
		try{
			amount = Double.parseDouble(text);
		}
		catch (NumberFormatException e){
			return;
		}
		Calendar cCalendar = GregorianCalendar.getInstance();
		String cTimeString = DateFormat.getDateInstance(DateFormat.MEDIUM)
				.format(cCalendar.getTime());
		manager.putData(type, amount, cTimeString, sign);
		Intent intent = new Intent(ActivityCount.this, MainActivity.class);
		startActivity(intent);
	}
	
	public void count(View view){
		int viewid = view.getId();
		String num = "";
		switch (viewid)
		{
			case R.id.btn_one:
				num = "1";
				break;
			case R.id.btn_two:
				num = "2";
				break;
			case R.id.btn_three:
				num = "3";
				break;
			case R.id.btn_four:
				num = "4";
				break;
			case R.id.btn_five:
				num = "5";
				break;
			case R.id.btn_six:
				num = "6";
				break;
			case R.id.btn_seven:
				num = "7";
				break;
			case R.id.btn_eight:
				num = "8";
				break;
			case R.id.btn_nine:
				num = "9";
				break;
			case R.id.btn_zero:
				num = "0";
				break;
			case R.id.btn_dot:
				num = ".";
				break;
		}
		String editnum = et.getText().toString();
		et.setText(editnum+num);
		
	}
	
	public void delete(View view)
	{
		
		String editnum = et.getText().toString();
		if (!editnum.equals(""))
			et.setText(editnum.substring(0,editnum.length()-1));
	}

}
